package tech.aistar.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

/**
 * 本类用来演示: "视图层" - 负责输出登录界面
 *
 * @author: success
 * @date: 2021/4/15 4:39 下午
 */
@WebServlet(urlPatterns = "/user/loginView")
public class UserViewController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();

        //接受一下error,有可能为null
        String error = req.getParameter("error");

        //获取所有的cookie
        String username = "";
        String password = "";

        Cookie[] cookies = req.getCookies();
        if(null!=cookies && cookies.length>0){
            for (Cookie cookie : cookies) {
                if("loginInfo".equals(cookie.getName())){
                    String value = cookie.getValue();
                    if(null!=value && value.length()>0) {
                        String[] arr = value.split(":");
                        username = URLDecoder.decode(URLDecoder.decode(arr[0], "utf-8"), "utf-8");
                        password = arr[1];
                    }
                }
            }
        }

        out.println("<!DOCTYPE html>");
        out.println("<html lang='en'>");
        out.println("<head>");
        out.println("<meta charset='UTF-8'>");
        out.println("<meta http-equiv='X-UA-Compatible' content='IE=edge'>");
        out.println("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
        out.println("<title>登录界面");out.println("</title>");
        out.println("<link rel='stylesheet' href='/aistar/plugins/bootstrap/css/bootstrap.min.css'>");
        out.println("<link rel='stylesheet' href='/aistar/css/login.css'>");
        out.println("</head>");
        out.println("<body>");
        out.println("<div class='container-fluid'>");
        out.println("<div class='row'>");
        out.println("<nav class='navbar navbar-inverse' role='navigation'>");
        out.println("<div class='container-fluid'>");
        out.println("<div class='navbar-header'>");
        out.println("<button type='button' class='navbar-toggle' data-toggle='collapse' data-target='#example-navbar-collapse'>");
                out.println("<span class='sr-only'>切换导航");
                out.println("</span>");
        out.println("<span class='icon-bar'>");out.println("</span>");
        out.println("<span class='icon-bar'>");out.println("</span>");
        out.println("<span class='icon-bar'>");out.println("</span>");
        out.println("</button>");
        out.println("<a class='navbar-brand' href='#'>菜鸟教程");
        out.println("</a>");
        out.println("</div>");
        out.println("<div class='collapse navbar-collapse' id='example-navbar-collapse'>");
        out.println("<ul class='nav navbar-nav'>");
        out.println("<li class='active'>");out.println("<a href='#'>iOS");
        out.println("</a>");out.println("</li>");
        out.println("<li>");out.println("<a href='#'>SVN");
        out.println("</a>");out.println("</li>");
        out.println("</ul>");
        out.println("</li>");
        out.println("</ul>");
        out.println("</div>");
        out.println("</div>");
        out.println("</nav>");
        out.println("</div>");
        out.println("</div>");

        out.println("<div class='container' id='login_div'>");
        out.println("<div class='row'>");
        out.println("<form action='/aistar/user/login' class='form-horizontal' role='form' method='post'>");
        out.println("<div class='form-group has-feedback'>");
        out.println("<label for='firstname' class='col-sm-2 control-label'>用户名");
        out.println("</label>");
        out.println("<div class='col-md-6'>");
        out.println("<input value='"+username+"' type='text' name='username' class='form-control' id='firstname' placeholder='请输入用户名'>");

        //判断error
        if("0".equals(error)){
            out.println("<span class='text-danger'>* 用户名不存在</span>");
        }


        out.println("<span class='glyphicon glyphicon-user form-control-feedback'>");out.println("</span>");
        out.println("</div>");
        out.println("</div>");
        out.println("<div class='form-group has-feedback'>");
        out.println("<label for='lastname' class='col-sm-2 control-label'>密码");out.println("</label>");
        out.println("<div class='col-md-6'>");
        out.println("<input value='"+password+"' type='password' name='password' class='form-control' id='lastname' placeholder='请输入密码'>");

        if("1".equals(error)) {
            out.println("<span class='text-danger'>* 密码错误</span>");
        }

        out.println("<span class='glyphicon glyphicon-lock form-control-feedback'>");out.println("</span>");
        out.println("</div>");
        out.println("</div>");
        out.println("<div class='form-group'>");
        out.println("<div class='col-sm-offset-2 col-sm-10'>");
        out.println("<div class='checkbox'>");
        out.println("<label>");
        out.println("<input type='checkbox' name='isForget' value='on'>请记住我");
        out.println("</label>");
        out.println("</div>");
        out.println("</div>");
        out.println("</div>");
        out.println("<div class='form-group'>");
        out.println("<div class='col-sm-offset-2 col-sm-10'>");
        out.println("<button type='submit' class='btn btn-default'>登录");out.println("</button>");
        out.println("</div>");
        out.println("</div>");
        out.println("</form>");
        out.println("</div>");
        out.println("</div>");

        out.println("<script src='/aistar/plugins/jquery/jquery.min.js'>");out.println("</script>");
        out.println("<script src='/aistar/plugins/bootstrap/js/bootstrap.min.js'>");out.println("</script>");
        out.println("</body>");
        out.println("</html>");




    }
}
